import ReconnectingWebSocket, { Options } from 'reconnecting-websocket'
export interface WebsocketProps {
	url: string
	config?: Options
	onOpen?: Function
	onClose?: Function
	onMessage?: Function
	onError?: Function
}
export const websocketConnect = ({ url, config, onOpen, onClose, onMessage, onError }: WebsocketProps) => {
	const rws = new ReconnectingWebSocket(url, [], config)
	rws.addEventListener('open', (e) => {
		onOpen && onOpen(e)
	})
	rws.addEventListener('close', (e) => {
		onClose && onClose(e)
	})
	rws.addEventListener('message', (e) => {
		onMessage && onMessage(e)
	})
	rws.addEventListener('error', (e) => {
		onError && onError(e)
	})
	return rws
}
export { ReconnectingWebSocket }
